/**
 * Created by Apple on 2016/7/13.
 */

'use strict';

var querystring = require("querystring");
var WeChatAPI = require("wechat-api");
var wxGZHconfig = require("./wx_config").wxGZHconfig;

var WXConfig = {
    token: wxGZHconfig.token,
    encodingAESKey:wxGZHconfig.encodingAESKey,
    
};

var WXConfigArray = [
    {
        appID: wxGZHconfig.appID,
        appSecret: wxGZHconfig.appSecret,
        id: wxGZHconfig.id,
        accessToken: wxGZHconfig.accessToken,
        name: wxGZHconfig.name,
        website: wxGZHconfig.website,
    }
];

var WXID2Info = {};
var Name2Info = {};
for (let i = 0; i < WXConfigArray.length; ++i) {

    let info = WXConfigArray[i];
    info.token = WXConfig.token;
    //console.log("+====="+wxGZHconfig.token);
    info.encodingAESKey = WXConfig.encodingAESKey;

    WXID2Info[info.id] = info;
    Name2Info[info.name] = info;
    info.api = new WeChatAPI(info.appID, info.appSecret);

    let updateAccessToken = function () {

        info.api.getAccessToken((err, accessToken) => {
            if (err) {
                console.log(err);
                return;
            }
            info.accessToken = accessToken.accessToken;

        });
    };
    updateAccessToken();

    setInterval(updateAccessToken, 60 * 1000 * 60);
}


module.exports = {WXID2Info: WXID2Info, Name2Info: Name2Info, WXConfig: WXConfig};


//处理如果是view的需要获取到openID的
function handleUrl(url, appID) {

    var url = querystring.escape(`https://m.${DOMAIN}/wxauth?url=${url}`);
    return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appID}&redirect_uri=${url}&response_type=code&scope=snsapi_base&state=${Math.random()}#wechat_redirect`;
}

function doCreateMenu(wxInfo) {

    wxInfo.api.createMenu({
        "button": [
            {
                "name": "下载地址",
                "sub_button": [
                    {
                        "type": "click",
                        "name": "获取下载网址",
                        "key": "GetDownUrl",

                    },

                    {
                        "type": "click",
                        "name": "获取二维码",
                        "key": "GetDownERcode",
                    },
                ]
            },
            {
                //"type": "click",
                "name": "推广盈利",
                //"key": "GetKefuERcode",
                //"key": "tuiguang",
                "sub_button": [
                    {
                        "type": "click",
                        "name": "推广介绍",
                        "key": "TuiguangDesc",
                    },
                    {
                        "type": "click",
                        "name": "我要推广",
                        //"key": "GetADLink",
                        "key": "GoTuiGuang",
                    },
                    {
                        "type": "view",
                        "name": "利润查询",
                        "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxee55786bb3fe378e&redirect_uri=http://cdndown.ac09u.com/wxshare/info&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect",
                    },
                ]
            },
            {

                "name": "更多",
                "sub_button": [
                    {
                        "type": "click",
                        "name": "活动介绍",
                        "key": "ActivityDesc",
                    },
                    {
                        "type": "click",
                        "name": "反馈留言",
                        "key": "Feedback",
                    },
                ]
            },

        ]
    }, (err, obj) => {
        console.log(JSON.stringify(obj, null, 4));
    });
}

for (var i = 0; i < WXConfigArray.length; ++i) {
    doCreateMenu(WXConfigArray[i]);
}

//
//
//
